package com.cinemtl.action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cinemtl.beans.Movie;
import com.cinemtl.manager.MovieManager;
import com.google.gson.Gson;

public class MovieAction {
	public static void showAllMovies(HttpServletRequest request,
			HttpServletResponse response) {

		ArrayList<Movie> movies = MovieManager.getAll();

		request.setAttribute("movies", movies);
	}
	
	public static String getMovieJsonById(int idMovie) {

		Gson gson = new Gson();
		
		Movie movie = MovieManager.getById(idMovie);
		String send = gson.toJson(movie);

		return send;
	}
	public static void showMovieById(HttpServletRequest request,
			HttpServletResponse response, int id) {

		Movie movie = MovieManager.getById(id);

		request.setAttribute("movie", movie);
		try {
			request.getRequestDispatcher("movies.jsp")
					.forward(request, response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void showMovieByType(HttpServletRequest request,
			HttpServletResponse response, String type) {

		ArrayList<Movie> movies = MovieManager.getByType(type);

		request.setAttribute("movies", movies);
		try {
			request.getRequestDispatcher("movies.jsp")
					.forward(request, response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void showMovieByTitle(HttpServletRequest request,
			HttpServletResponse response, String title) {

		Movie movie = MovieManager.getByTitle(title);

		request.setAttribute("movie", movie);
		try {
			request.getRequestDispatcher("movies.jsp")
					.forward(request, response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void searchMovieByTitle(HttpServletRequest request,
			HttpServletResponse response, String title) {

		ArrayList<Movie> movies = MovieManager.searchMovie(title);

		try {
			request.setAttribute("searchMovies", movies);
			request.getRequestDispatcher("search.jsp").forward(request,	response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void insertMovie(Movie movie)
	{
		MovieManager.insertMovie(movie);
	}
	
	public static void updateMovie(Movie movie)
	{
		MovieManager.updateMovie(movie);
	}
	
	public static void deleteMovie(int idMovie)
	{
		MovieManager.deleteMovie(idMovie);
	}
}
